//
//  LHPCountDownButton.m
//  TestDemo-button
//
//  Created by 李洪攀 on 2018/7/25.
//  Copyright © 2018年 李洪攀. All rights reserved.
//

#import "LHPCountDownButton.h"

@interface LHPCountDownButton(){
    NSInteger _second;
    NSUInteger _totalSecond;
    
    NSTimer *_timer;
    NSDate *_startDate;
    
    CountDownChanging _countDownChanging;
    CountDownFinished _countDownFinished;
    TouchedCountDownButtonHandler _touchCountDownButtonHandler;
    
}
@end

@implementation LHPCountDownButton

#pragma mark - touch action
- (void)countDownButtonHandler:(TouchedCountDownButtonHandler)touchedCountDownButtonHandler
{
    _touchCountDownButtonHandler = [touchedCountDownButtonHandler copy];
    [self addTarget:self action:@selector(touched:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - btn selector
- (void)touched:(LHPCountDownButton *)sender
{
    if (_touchCountDownButtonHandler) {
        _touchCountDownButtonHandler(sender,sender.tag);
    }
}

#pragma mark - set second
- (void)startCountDownWithSecond:(NSInteger)second
{
    _totalSecond = second;
    _second = second;
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerStart:) userInfo:nil repeats:YES];
    _startDate = [NSDate date];
    [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
}

#pragma mark - timer selector
- (void)timerStart:(NSTimer *)timer
{
    double dateTime = [[NSDate date] timeIntervalSinceDate:_startDate];
    
    _second = _totalSecond - (NSInteger)(dateTime+0.5);
    
    if (_second<0.0) {
        [self stopCountDown];
    }else{
        if (_countDownChanging) {
            [self setTitle:_countDownChanging(self,_second) forState:UIControlStateNormal];
            [self setTitle:_countDownChanging(self,_second) forState:UIControlStateDisabled];
        }else{
            NSString *title = [NSString stringWithFormat:@"%zd秒",_second];
            [self setTitle:title forState:UIControlStateNormal];
            [self setTitle:title forState:UIControlStateDisabled];
        }
    }
}

#pragma mark - stop count down
- (void)stopCountDown
{
    if (_timer) {
        if ([_timer respondsToSelector:@selector(isValid)]) {
            if ([_timer isValid]) {
                [_timer invalidate];
                _second = _totalSecond;
                if (_countDownFinished)  {
                    [self setTitle:_countDownFinished(self,_totalSecond) forState:UIControlStateNormal];
                    [self setTitle:_countDownFinished(self,_totalSecond) forState:UIControlStateDisabled];
                }else{
                    [self setTitle:@"重新获取" forState:UIControlStateNormal];
                    [self setTitle:@"重新获取" forState:UIControlStateDisabled];
                }
            }
        }
    }
}

#pragma mark - block setting
-(void)countDownChanging:(CountDownChanging)countDownChanging
{
    _countDownChanging = [countDownChanging copy];
}

-(void)countDownFinished:(CountDownFinished)countDownFinished
{
    _countDownFinished = [countDownFinished copy];
}
@end
